home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16.lha / Python-1.6 / Lib / Python1.6 / distutils / sysconfig.py < prev    next >
Encoding:
Python Source  |  2000-08-02  |  9.8 KB  |  305 lines

  1. """Provide access to Python's configuration information.  The specific names
  2. defined in the module depend heavily on the platform and configuration.
  3.  
  4. Written by:   Fred L. Drake, Jr.
  5. Email:        <fdrake@acm.org>
  6. Initial date: 17-Dec-1998
  7. """
  8.  
  9. __revision__ = "$Id: sysconfig.py,v 1.24 2000/08/02 01:49:40 gward Exp $"
  10.  
  11. import os
  12. import re
  13. import string
  14. import sys
  15.  
  16. from errors import DistutilsPlatformError
  17.  
  18.  
  19. PREFIX = os.path.normpath(sys.prefix)
  20. EXEC_PREFIX = os.path.normpath(sys.exec_prefix)
  21.  
  22.  
  23. def get_python_inc(plat_specific=0, prefix=None):
  24.     """Return the directory containing installed Python header files.
  25.  
  26.     If 'plat_specific' is false (the default), this is the path to the
  27.     non-platform-specific header files, i.e. Python.h and so on;
  28.     otherwise, this is the path to platform-specific header files
  29.     (namely config.h).
  30.  
  31.     If 'prefix' is supplied, use it instead of sys.prefix or
  32.     sys.exec_prefix -- i.e., ignore 'plat_specific'.
  33.     """    
  34.     if prefix is None:
  35.         prefix = (plat_specific and EXEC_PREFIX or PREFIX)
  36.     if os.name == "posix":
  37.         return os.path.join(prefix, "include", "python" + sys.version[:3])
  38.     elif os.name == "nt":
  39.         return os.path.join(prefix, "Include") # include or Include?
  40.     elif os.name == "mac":
  41.         return os.path.join(prefix, "Include")
  42.     else:
  43.         raise DistutilsPlatformError, \
  44.               ("I don't know where Python installs its C header files " +
  45.                "on platform '%s'") % os.name
  46.  
  47.  
  48. def get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
  49.     """Return the directory containing the Python library (standard or
  50.     site additions).
  51.  
  52.     If 'plat_specific' is true, return the directory containing
  53.     platform-specific modules, i.e. any module from a non-pure-Python
  54.     module distribution; otherwise, return the platform-shared library
  55.     directory.  If 'standard_lib' is true, return the directory
  56.     containing standard Python library modules; otherwise, return the
  57.     directory for site-specific modules.
  58.  
  59.     If 'prefix' is supplied, use it instead of sys.prefix or
  60.     sys.exec_prefix -- i.e., ignore 'plat_specific'.
  61.     """
  62.     if prefix is None:
  63.         prefix = (plat_specific and EXEC_PREFIX or PREFIX)
  64.        
  65.     if os.name == "posix":
  66.         libpython = os.path.join(prefix,
  67.                                  "lib", "python" + sys.version[:3])
  68.         if standard_lib:
  69.             return libpython
  70.         else:
  71.             return os.path.join(libpython, "site-packages")
  72.  
  73.     elif os.name == "nt":
  74.         if standard_lib:
  75.             return os.path.join(PREFIX, "Lib")
  76.         else:
  77.             return prefix
  78.  
  79.     elif os.name == "mac":
  80.         if plat_specific:
  81.             if standard_lib:
  82.                 return os.path.join(EXEC_PREFIX, "Mac", "Plugins")
  83.             else:
  84.                 raise DistutilsPlatformError, \
  85.                       "OK, where DO site-specific extensions go on the Mac?"
  86.         else:
  87.             if standard_lib:
  88.                 return os.path.join(PREFIX, "Lib")
  89.             else:
  90.                 raise DistutilsPlatformError, \
  91.                       "OK, where DO site-specific modules go on the Mac?"
  92.     else:
  93.         raise DistutilsPlatformError, \
  94.               ("I don't know where Python installs its library " +
  95.                "on platform '%s'") % os.name
  96.  
  97. # get_python_lib()
  98.         
  99.  
  100. def customize_compiler (compiler):
  101.     """Do any platform-specific customization of the CCompiler instance
  102.     'compiler'.  Mainly needed on Unix, so we can plug in the information
  103.     that varies across Unices and is stored in Python's Makefile.
  104.     """
  105.     if compiler.compiler_type == "unix":
  106.         cc_cmd = CC + ' ' + OPT
  107.         compiler.set_executables(
  108.             preprocessor=CC + " -E",    # not always!
  109.             compiler=cc_cmd,
  110.             compiler_so=cc_cmd + ' ' + CCSHARED,
  111.             linker_so=LDSHARED,
  112.             linker_exe=CC)
  113.  
  114.         compiler.shared_lib_extension = SO
  115.  
  116.  
  117. def get_config_h_filename():
  118.     """Return full pathname of installed config.h file."""
  119.     inc_dir = get_python_inc(plat_specific=1)
  120.     return os.path.join(inc_dir, "config.h")
  121.  
  122.  
  123. def get_makefile_filename():
  124.     """Return full pathname of installed Makefile from the Python build."""
  125.     lib_dir = get_python_lib(plat_specific=1, standard_lib=1)
  126.     return os.path.join(lib_dir, "config", "Makefile")
  127.  
  128.  
  129. def parse_config_h(fp, g=None):
  130.     """Parse a config.h-style file.
  131.  
  132.     A dictionary containing name/value pairs is returned.  If an
  133.     optional dictionary is passed in as the second argument, it is
  134.     used instead of a new dictionary.
  135.     """
  136.     if g is None:
  137.         g = {}
  138.     define_rx = re.compile("#define ([A-Z][A-Z0-9_]+) (.*)\n")
  139.     undef_rx = re.compile("/[*] #undef ([A-Z][A-Z0-9_]+) [*]/\n")
  140.     #
  141.     while 1:
  142.         line = fp.readline()
  143.         if not line:
  144.             break
  145.         m = define_rx.match(line)
  146.         if m:
  147.             n, v = m.group(1, 2)
  148.             try: v = string.atoi(v)
  149.             except ValueError: pass
  150.             g[n] = v
  151.         else:
  152.             m = undef_rx.match(line)
  153.             if m:
  154.                 g[m.group(1)] = 0
  155.     return g
  156.  
  157. def parse_makefile(fp, g=None):
  158.     """Parse a Makefile-style file.
  159.  
  160.     A dictionary containing name/value pairs is returned.  If an
  161.     optional dictionary is passed in as the second argument, it is
  162.     used instead of a new dictionary.
  163.  
  164.     """
  165.     if g is None:
  166.         g = {}
  167.     variable_rx = re.compile("([a-zA-Z][a-zA-Z0-9_]+)\s*=\s*(.*)\n")
  168.     done = {}
  169.     notdone = {}
  170.     #
  171.     while 1:
  172.         line = fp.readline()
  173.         if not line:
  174.             break
  175.         m = variable_rx.match(line)
  176.         if m:
  177.             n, v = m.group(1, 2)
  178.             v = string.strip(v)
  179.             if "$" in v:
  180.                 notdone[n] = v
  181.             else:
  182.                 try: v = string.atoi(v)
  183.                 except ValueError: pass
  184.                 done[n] = v
  185.  
  186.     # do variable interpolation here
  187.     findvar1_rx = re.compile(r"\$\(([A-Za-z][A-Za-z0-9_]*)\)")
  188.     findvar2_rx = re.compile(r"\${([A-Za-z][A-Za-z0-9_]*)}")
  189.     while notdone:
  190.         for name in notdone.keys():
  191.             value = notdone[name]
  192.             m = findvar1_rx.search(value)
  193.             if not m:
  194.                 m = findvar2_rx.search(value)
  195.             if m:
  196.                 n = m.group(1)
  197.                 if done.has_key(n):
  198.                     after = value[m.end():]
  199.                     value = value[:m.start()] + done[n] + after
  200.                     if "$" in after:
  201.                         notdone[name] = value
  202.                     else:
  203.                         try: value = string.atoi(value)
  204.                         except ValueError: pass
  205.                         done[name] = string.strip(value)
  206.                         del notdone[name]
  207.                 elif notdone.has_key(n):
  208.                     # get it on a subsequent round
  209.                     pass
  210.                 else:
  211.                     done[n] = ""
  212.                     after = value[m.end():]
  213.                     value = value[:m.start()] + after
  214.                     if "$" in after:
  215.                         notdone[name] = value
  216.                     else:
  217.                         try: value = string.atoi(value)
  218.                         except ValueError: pass
  219.                         done[name] = string.strip(value)
  220.                         del notdone[name]
  221.             else:
  222.                 # bogus variable reference; just drop it since we can't deal
  223.                 del notdone[name]
  224.  
  225.     # save the results in the global dictionary
  226.     g.update(done)
  227.     return g
  228.  
  229.  
  230. def _init_posix():
  231.     """Initialize the module as appropriate for POSIX systems."""
  232.     g = globals()
  233.     # load the installed Makefile:
  234.     try:
  235.         filename = get_makefile_filename()
  236.         file = open(filename)
  237.     except IOError, msg:
  238.         my_msg = "invalid Python installation: unable to open %s" % filename
  239.         if hasattr(msg, "strerror"):
  240.             my_msg = my_msg + " (%s)" % msg.strerror
  241.  
  242.         raise DistutilsPlatformError, my_msg
  243.               
  244.     parse_makefile(file, g)
  245.     
  246.     # On AIX, there are wrong paths to the linker scripts in the Makefile
  247.     # -- these paths are relative to the Python source, but when installed
  248.     # the scripts are in another directory.
  249.     if sys.platform == 'aix4':          # what about AIX 3.x ?
  250.         # Linker script is in the config directory, not in Modules as the
  251.         # Makefile says.
  252.         python_lib = get_python_lib(standard_lib=1)
  253.         ld_so_aix = os.path.join(python_lib, 'config', 'ld_so_aix')
  254.         python_exp = os.path.join(python_lib, 'config', 'python.exp')
  255.  
  256.         g['LDSHARED'] = "%s %s -bI:%s" % (ld_so_aix, g['CC'], python_exp)
  257.  
  258.  
  259. def _init_nt():
  260.     """Initialize the module as appropriate for NT"""
  261.     g = globals()
  262.     # set basic install directories
  263.     g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
  264.     g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
  265.  
  266.     # XXX hmmm.. a normal install puts include files here
  267.     g['INCLUDEPY'] = get_python_inc(plat_specific=0)
  268.  
  269.     g['SO'] = '.pyd'
  270.     g['EXE'] = ".exe"
  271.     g['exec_prefix'] = EXEC_PREFIX
  272.  
  273.  
  274. def _init_mac():
  275.     """Initialize the module as appropriate for Macintosh systems"""
  276.     g = globals()
  277.     # set basic install directories
  278.     g['LIBDEST'] = get_python_lib(plat_specific=0, standard_lib=1)
  279.     g['BINLIBDEST'] = get_python_lib(plat_specific=1, standard_lib=1)
  280.  
  281.     # XXX hmmm.. a normal install puts include files here
  282.     g['INCLUDEPY'] = get_python_inc(plat_specific=0)
  283.  
  284.     g['SO'] = '.ppc.slb'
  285.     g['exec_prefix'] = EXEC_PREFIX
  286.     print sys.prefix, PREFIX
  287.  
  288.     # XXX are these used anywhere?
  289.     g['install_lib'] = os.path.join(EXEC_PREFIX, "Lib")
  290.     g['install_platlib'] = os.path.join(EXEC_PREFIX, "Mac", "Lib")
  291.  
  292.  
  293. try:
  294.     exec "_init_" + os.name
  295. except NameError:
  296.     # not needed for this platform
  297.     pass
  298. else:
  299.     exec "_init_%s()" % os.name
  300.  
  301.  
  302. del _init_posix
  303. del _init_nt
  304. del _init_mac
  305.